package com.example.windowdemo.window.statewindow;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.example.windowdemo.R;

/**
 * @author: njb
 * @date: 2023/8/2 17:58
 * @desc:
 */
public class WindowState<T> {
    private static final String TAG = "WindowLog";
    private final Context context;
    private final WindowManager.LayoutParams layoutParams;
    private final View contentView;
    private WindowState state;
    private boolean isFullScreen;
    private boolean hasTitleBar;
    private int width;
    private int height;
    private T data;

    public WindowState(Context context) {
        this.context = context;
        this.layoutParams = new WindowManager.LayoutParams();
        this.contentView = LayoutInflater.from(context).inflate(R.layout.my_window_layout, null);
    }

    public void setFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
        if (isFullScreen) {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags &= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setTitleBar(boolean hasTitleBar) {
        this.hasTitleBar = hasTitleBar;
        if (hasTitleBar) {
            layoutParams.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
        layoutParams.width = width;
        layoutParams.height = height;
    }

    public void setPosition(int x, int y) {
        layoutParams.x = x;
        layoutParams.y = y;
    }

    public void setGravity(int gravity) {
        layoutParams.gravity = gravity;
    }

    public void setState(WindowState state) {
        this.state = state;
    }

    public void setData(T data) {
        Log.d(TAG, "setData() called with data: " + data);
    }

    public void show() {
        state.show();
    }

    public void hide() {
        state.hide();
    }

    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        return state.dispatchKeyEvent(keyEvent);
    }

    public WindowManager.LayoutParams getLayoutParams() {
        return layoutParams;
    }

    public View getContentView() {
        return contentView;
    }

    public Context getContext() {
        return context;
    }

}
